/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.common;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Neo
 */
@ManagedBean
@ViewScoped
public class FileDownloadBean extends AbstractUIBean implements Serializable {

    /**
     * Creates a new instance of FileDownloadBean
     */
    public FileDownloadBean() {
    }
//    private String path;
    private String contentType;

    public void downloadAction(String path) {
        path = path;
        contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(path);
    }

    public StreamedContent loadFile(String path, String fileName) throws IOException {
        contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(path);
        return new DefaultStreamedContent(new FileInputStream(path), contentType, fileName);
    }

    public StreamedContent getAdmissionForm() {
        String path = APP_DOMAIN + "/resources/fileupload/admisstion/admission.jpg";
        contentType = FacesContext.getCurrentInstance().getExternalContext().getMimeType(path);
        try {
            return new DefaultStreamedContent(new FileInputStream(path), contentType, "Admission form download");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileDownloadBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
